package com.sss.controller.goods;

import com.alibaba.fastjson.JSONObject;
import com.sss.controller.BaseController;
import com.sss.entity.Goods;
import com.sss.service.GoodsService;
import com.sss.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsMangeController extends BaseController {
    @Resource(name = "goodsService")
    private GoodsService goodsService;

    //查询
    @RequestMapping(value = "goods/goodsmanage",method = RequestMethod.GET)
    public String mange(HttpSession session, Map<String,Object> map) {
        List<Goods> goodsList =new ArrayList<Goods>();
        logger.info("检查管理员权限");
        Object adminId=checkAdmin(session);
        if (adminId==null){
            return "redirect:/admin/login";
        }
        goodsList= goodsService.getList();
        map.put("goodslist", goodsList);
        for (int i = 0; i < goodsList.size(); i++) {
            logger.info(goodsList.get(i));
        }
        return "goods/goodslist";
    }
    //编辑功能
    @ResponseBody
    @RequestMapping(value = "goods/edit",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String edit(HttpSession session, @RequestParam Integer id, @RequestParam String name,@RequestParam String number){
        logger.info("检查管理员权限");
        Object adminId=checkAdmin(session);
        if (adminId==null){
            return "redirect:/admin/login";
        }
        Goods goods=new Goods(id,name,number);
        boolean IsSuccuess=goodsService.update(goods);
        JSONObject object=new JSONObject();
        if (IsSuccuess){
            logger.info("物品修改成功"+id+" 名字"+name+" 数量"+number);

            object.put("statusGoods","success");
        }else{
            logger.info("物品修改错误");
            object.put("statusGoods","false");
        }
        return object.toJSONString();
    }
    //添加
    @ResponseBody
    @RequestMapping(value = "goods/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String add(HttpSession session,  @RequestParam String name,@RequestParam String number){
        logger.info("检查管理员权限");
        Object adminId=checkAdmin(session);
        if (adminId==null){
            return "redirect:/admin/login";
        }
        Goods goods=new Goods(null,name,number);
        boolean IsSuccuess=goodsService.add(goods);
        JSONObject object=new JSONObject();
        if (IsSuccuess){
            logger.info("物品添加成功"+ "名字"+name+" 数量"+number);

            object.put("statusGoods","success");
        }else{
            logger.info("物品添加错误");
            object.put("statusGoods","false");
        }
        return object.toJSONString();
    }
    //删除
    @ResponseBody
    @RequestMapping(value = "goods/delete",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String delete(HttpSession session,  @RequestParam Integer id){
        logger.info("检查管理员权限");
        Integer IsSuccess=goodsService.deleteOne(id);
        Object adminId=checkAdmin(session);
        if (adminId==null){
            return "redirect:/admin/login";
        }
        JSONObject object=new JSONObject();
        if (IsSuccess==1){
            logger.info("物品删除成功"+ "id"+id);

            object.put("statusGoods","success");
        }else{
            logger.info("物品删除错误");
            object.put("statusGoods","false");
        }
        return object.toJSONString();
    }
}
